﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ComLib.Data;
using ComLib.Web.Modules;
using ComLib.Web.Modules.Posts;
using ComLib.Web.Modules.Tags;
using ComLib.Web.Lib.Controllers;
using ComLib.Web.Lib.Models;
using ComLib.Web.Lib.Helpers;


namespace ComLib.CMS.Controllers
{
    /// <summary>
    /// Controller for Dynamic Html content pages.
    /// </summary>
    [HandleError]
    public class PostController : EntityControllerAutomaticDataBinding<Post>, IEntityController
    {
        /// <summary>
        /// Find posts by tags.
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ActionResult Tags(string tag)
        {
            IList<Post> posts = Post.GetByTags(tag);
            return View("Pages/List", PagerHelper.ConvertToListViewModel<Post>(posts, string.Empty, this.Conf));
        }


        /// <summary>
        /// Find posts by year/month.
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public ActionResult Archives(int year, int month)
        {   
            IList<Post> posts = Post.Find(Query<Post>.New().Where(p => p.Year).Is(year)
                                                              .And(p => p.Month).Is(month));
            ViewResult v = View("Pages/List", PagerHelper.ConvertToListViewModel<Post>(posts, string.Empty, this.Conf));            
            return v;
        }
    }
}
